/*
 *  Copyright 2011 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.vcforms.dtseditor;

import org.xml.sax.SAXException;
import org.xml.sax.Attributes;

/**
 *
 * @author yura
 */
public class XmlUtil extends org.opu.vcforms.transform_t2d.util.XmlUtil {

    public byte[] rmCustomTags(byte[] data){
        RmCustomTags handler = new RmCustomTags();
        parse(data, handler);
        return handler.accumulator.toString().getBytes();
    }

    public byte[] toHTML(byte[] data){
        ToHTML handler = new ToHTML();
        parse(data, handler);
        return handler.accumulator.toString().getBytes();
    }

    class ToHTML extends org.xml.sax.helpers.DefaultHandler {
        StringBuilder accumulator = new StringBuilder();
        private boolean txt = false;

        @Override
        public void characters(char[] buffer, int start, int length) {
            if (txt) {
                accumulator.append(buffer, start, length);
            }
        }

        @Override
        public void startElement(String uri, String localName, String qName,
                Attributes atrbts) throws SAXException {
            final String[] arr = qName.split("\\:", 2);

            if (arr.length > 1 && "document".equals(arr[1])){
                accumulator.append("<html>");
            } else if (arr.length > 1 && "body".equals(arr[1])){
                accumulator.append("<body>");
            } else if (arr.length > 1 && "p".equals(arr[1])){
                accumulator.append("<p>");
            } else if (arr.length > 1 && "t".equals(arr[1])){
                txt = true;
            } else if (arr.length > 1 && "tbl".equals(arr[1])){
                accumulator.append("<table border=1>");
            } else if (arr.length > 1 && "tr".equals(arr[1])){
                accumulator.append("<tr>");
            } else if (arr.length > 1 && "tc".equals(arr[1])){
                accumulator.append("<td>");
            } else if (arr.length > 1 && "smartTag".equals(arr[1])){
                accumulator.append("<b>[</b><font color=blue>");
            }
        }

        @Override
        public void endElement(String uri, String localName, String qName)
                throws SAXException {
            final String[] arr = qName.split("\\:", 2);

            if (arr.length > 1 && "document".equals(arr[1])){
                accumulator.append("</html>");
            } else if (arr.length > 1 && "body".equals(arr[1])){
                accumulator.append("</body>");
            } else if (arr.length > 1 && "p".equals(arr[1])){
                accumulator.append("</p>");
            } else if (arr.length > 1 && "t".equals(arr[1])){
                txt = false;
            } else if (arr.length > 1 && "tbl".equals(arr[1])){
                accumulator.append("</table>");
            } else if (arr.length > 1 && "tr".equals(arr[1])){
                accumulator.append("</tr>");
            } else if (arr.length > 1 && "tc".equals(arr[1])){
                accumulator.append("</td>");
            } else if (arr.length > 1 && "smartTag".equals(arr[1])){
                accumulator.append("</font><b>]</b>");
            }
        }
    }

    class RmCustomTags extends DefaultHandler {
        public static final String CUSTOM_XML_TAG = "customXml";
        public static final String CUSTOM_XML_PR_TAG = "customXmlPr";
        public static final String ATTR_TAG = "attr";
        
        StringBuilder accumulator = new StringBuilder();
        private boolean lastCustomXMl = false;

        @Override
        public void characters(char[] buffer, int start, int length) {
            append(new String(buffer, start, length));
        }

        @Override
        public void startElement(String uri, String localName, String qName,
                Attributes atrbts) throws SAXException {
            final String[] arr = qName.split("\\:", 2);

            if (arr.length > 1 && CUSTOM_XML_TAG.equals(arr[1])){
                lastCustomXMl = true;
                return;
            } else if (arr.length > 1 && CUSTOM_XML_PR_TAG.equals(arr[1])){
                return;
            } else if (arr.length > 1 && ATTR_TAG.equals(arr[1]) && lastCustomXMl){
                return;
            }

            openTag(uri, localName, qName, atrbts);
        }

        @Override
        public void endElement(String uri, String localName, String qName)
                throws SAXException {
            final String[] arr = qName.split("\\:", 2);

            if (arr.length > 1 && CUSTOM_XML_TAG.equals(arr[1])){
                lastCustomXMl = false;
                return ;
            } else if (arr.length > 1 && CUSTOM_XML_PR_TAG.equals(arr[1])){
                return;
            } else if (arr.length > 1 && ATTR_TAG.equals(arr[1]) && lastCustomXMl){
                return;
            }

            closeTag(uri, localName, qName);
        }

        @Override
        protected void append(CharSequence str) {
            accumulator.append(str);
        }
    }
}
